package com.catsic;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.esri.android.map.Callout;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.core.geometry.Geometry.Type;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;

public class QueryTaskActivity extends Activity{
	
	MapView map;
	EditText et_lxbm;
	Button queryBtn;
	GraphicsLayer gl;
	Callout callout;

	/** Called when the activity is first created. */

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		map = (MapView) findViewById(R.id.map);
		queryBtn = (Button) findViewById(R.id.querybtn);
		et_lxbm = (EditText) findViewById(R.id.tv_lxbm);
		
		map.addLayer(new ArcGISDynamicMapServiceLayer(getResources().getString(R.string.basemap_url)));
		map.addLayer(new ArcGISDynamicMapServiceLayer(getResources().getString(R.string.operationallayer_url)));

		gl = new GraphicsLayer();
		map.addLayer(gl);

		queryBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				Query query = new Query();
				
//				query.setWhere("LXBM='"+et_lxbm.getText().toString()+"'");
				query.setWhere("LXBM='C622430423'");

				query.setReturnGeometry(true);
				String[] outfields = new String[] { "OBJECTID", "LXBM","LXMC", "QDZH","ZDZH"};
				query.setOutFields(outfields);
				query.setOutSpatialReference(map.getSpatialReference());

				Query[] queryParams = { query };
				AsyncQueryTask qt = new AsyncQueryTask();
				qt.execute(queryParams);
				
			}
		});
		
	}
	
	private View loadView(String lxbm, String lxmc) {
		View view = LayoutInflater.from(this).inflate(R.layout.sqmi, null);

		final TextView name = (TextView) view.findViewById(R.id.county_name);
		name.setText(lxbm);

		final TextView number = (TextView) view.findViewById(R.id.pop_sqmi);
		number.setText(lxmc);

		final ImageView photo = (ImageView) view.findViewById(R.id.family_photo);
		photo.setImageDrawable(this.getResources().getDrawable(R.drawable.family));

		return view;

	}

	private class AsyncQueryTask extends AsyncTask<Query, Void, FeatureSet> {

		protected FeatureSet doInBackground(Query... params) {

			if (params.length > 0) {
				Query query = params[0];
				QueryTask queryTask = new QueryTask(getResources().getString(R.string.operationallayer_url)+"/10");
				try {
					FeatureSet fs = queryTask.execute(query);
					return fs;
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}

			return null;
		}

		protected void onPostExecute(FeatureSet fs) {
			
			if (fs==null || fs.getGraphics()==null || fs.getGraphics().length==0) {
				return ;
			}
			
			for (Graphic graphic: fs.getGraphics()) {
				
				PictureMarkerSymbol pic = new PictureMarkerSymbol(getResources().getDrawable(R.drawable.family));
				
				SimpleMarkerSymbol sms = new SimpleMarkerSymbol(Color.RED, 25,STYLE.CIRCLE);
				
				SimpleLineSymbol sls = new SimpleLineSymbol(Color.RED, 25, com.esri.core.symbol.SimpleLineSymbol.STYLE.SOLID);
				
				
				Graphic nGraphic = new Graphic(graphic.getGeometry(),sls,graphic.getAttributes());
				gl.addGraphic(nGraphic);
				
			}
			
			Graphic  graphic = fs.getGraphics()[0];
			Type  type =  graphic.getGeometry().getType();
			if (type.POLYLINE.equals(Type.POLYLINE)) {
				Polyline polyline =  (Polyline) graphic.getGeometry();
				map.zoomTo(polyline.getPoint(0), 100f);
			}
			
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		map.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		map.unpause();
	}

}